#include <sysdep.h>
#include <number.h>
#include <log.h>
#include <shell.h>

#define CALC_VERSION	"1.0"
#define CALC_NAME	"calc"
#ifdef WIN32
#define CALC_SYSTEM	"win32"
#else
#define CALC_SYSTEM	"unix"
#endif

#define CALC_DEF_CONF_FILE		"../conf/calc.conf"
/* #define CALC_DEF_CONF_FILE		"/etc/"CALC_NAME".conf" */

const char *calc_conf = CALC_DEF_CONF_FILE;
int gloop = 1;

static int calc_argcv(int argc, char **argv)
{
	int argval;

	/* process the options */
	while ((argval = getopt(argc, argv, "f:")) != EOF) {
		switch (argval) {
		case 'f':
			calc_conf = optarg;
			break;
		default:
			break;
		}
	}
	return CALC_E_SUCCESS;
}

int main_init(int argc, char **argv)
{
	if (CALC_E_SUCCESS != calc_argcv(argc, argv)) {
		calc_log(LOG_MAIN_DEBUG, "init failure at 1st step.");
		return CALC_E_INIT_FAILURE;
	}

	if (CALC_E_SUCCESS != shell_init(calc_conf)) {
		calc_log(LOG_MAIN_DEBUG, "init failure at 2nd step.");
		return CALC_E_INIT_FAILURE;
	}

	calc_log(LOG_MAIN_DEBUG, "init completed");
	return CALC_E_SUCCESS;
}

void main_exit(void)
{
	shell_exit();
}

void main_test(void)
{
//	number_test();
	shell_test();
	printf("\n");
}

void main_loop(void)
{
	while (gloop)
		;
}

int main(int argc, char **argv)
{
	atexit(main_exit);

	if (main_init(argc, argv) != CALC_E_SUCCESS)
		return 0;
	main_test();

	main_loop();

	return 0;
}
